﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NomClasses;

namespace NomCafe
{
    public partial class frmShiftManager : DevComponents.DotNetBar.Office2007RibbonForm
    {
        string nameShift;

        public frmShiftManager()
        {
            InitializeComponent();
        }

        private void frmShiftManager_Load(object sender, EventArgs e)
        {
            inputTimeBegin.Value = DateTime.Now;
            inputTimeEnd.Value = DateTime.Now;

            getDataShift();
        }

        private void getDataShift()
        {
            listViewShift.Items.Clear();
            // Lấy danh sách ca làm việc cho listViewShift
            NomClasses.NomDataContext nc = new NomClasses.NomDataContext();
            System.Data.Linq.Table<NomClasses.nom_shift> shiftList = nc.nom_shifts;
            foreach (NomClasses.nom_shift shift in shiftList)
            {
                string[] row = {shift.time_begin.ToString() , shift.time_end.ToString() };
                listViewShift.Items.Add(shift.name).SubItems.AddRange(row);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void listViewShift_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnDelete.Enabled = true;
            btnEdit.Enabled = true;
            btnAccept.Enabled = false;

            nameShift = "";
            for (int i = 0; i < listViewShift.Items.Count; i++)
            {
                if (listViewShift.Items[i].Selected)
                {
                    nameShift = listViewShift.Items[i].SubItems[0].Text;
                    txtNameShift.Text = nameShift;
                    inputTimeBegin.Value = Convert.ToDateTime(listViewShift.Items[i].SubItems[1].Text);
                    inputTimeEnd.Value = Convert.ToDateTime(listViewShift.Items[i].SubItems[2].Text);
                }
            }
        }

        private void btnRenew_Click(object sender, EventArgs e)
        {
            getDataShift();

            txtNameShift.Text = "";
            inputTimeBegin.Value = DateTime.Now;
            inputTimeEnd.Value = DateTime.Now; 

            btnDelete.Enabled = false;
            btnEdit.Enabled = false;
            btnAccept.Enabled = true;
            btnRenew.Enabled = true;
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            bool flag = false;
            try
            {
                if (txtNameShift.Text != "")
                {
                    NomDataContext nc = new NomDataContext();
                    var shiftList = from shift in nc.nom_shifts
                                   select shift;
                    foreach (var i in shiftList)
                    {
                        if (txtNameShift.Text.Trim().ToLower().Equals(i.name.Trim().ToLower()))
                        {
                            flag = true;
                        }
                    }
                    if (flag == true)
                    {
                        MessageBox.Show(languageManager.ALREADY_SHIFT_ERROR, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        nom_shift shift = new nom_shift();
                        shift.name = txtNameShift.Text;
                        shift.time_begin = inputTimeBegin.Value.ToShortTimeString();
                        shift.time_end = inputTimeEnd.Value.ToShortTimeString();
                        nc.nom_shifts.InsertOnSubmit(shift);
                        nc.SubmitChanges();

                        getDataShift();

                        btnRenew_Click(sender, e);
                    }
                }
                else
                {
                    MessageBox.Show(languageManager.INSERT_SHIFT_ERROR, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                NomDataContext nc = new NomDataContext();
                nom_shift shift = nc.nom_shifts.Single(i => i.name == nameShift);
                nc.nom_shifts.DeleteOnSubmit(shift);
                nc.SubmitChanges();

                btnRenew_Click(sender, e);
            }
            catch (Exception)
            {
                MessageBox.Show(languageManager.DELETE_SHIFT_ERROR, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            bool flag = false;
            try
            {
                NomDataContext nc = new NomDataContext();
                var shiftList = from shift in nc.nom_shifts
                                where shift.name != nameShift
                                select shift;
                foreach (var i in shiftList)
                {
                    if (txtNameShift.Text.Trim().ToLower().Equals(i.name.Trim().ToLower()))
                    {
                        flag = true;
                    }
                }
                if (flag == true)
                {
                    MessageBox.Show(languageManager.ALREADY_SHIFT_ERROR, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {                    
                    nom_shift shift = nc.nom_shifts.Single(i => i.name == nameShift);
                    shift.name = txtNameShift.Text;
                    shift.time_begin = inputTimeBegin.Value.ToShortTimeString();
                    shift.time_end = inputTimeEnd.Value.ToShortTimeString();
                    nc.SubmitChanges();
                                        
                    btnRenew_Click(sender,e);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}
